package com.zhang.zhangapp.view;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhang.zhangapp.R;
import com.zhang.zhangapp.base.http.MCallback;
import com.zhang.zhangapp.bean.user.SearchHistoryDao;
import com.zhang.zhangapp.http.CommonHttp;
import com.zhang.zhangapp.view.custom.SearchView;
import com.zhang.zhangapp.view.shop.ShopListActivity;

import org.xutils.common.Callback;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by 罗肖 on 2018/4/10.
 */
@ContentView(R.layout.activity_search)
public class SearchActivity extends BaseAppCompatActivityActivity {

    @ViewInject(R.id.search_text)
    TextView searchText;
    @ViewInject(R.id.search_edit)
    EditText searchEdit;
    @ViewInject(R.id.search_history_view)
    LinearLayout historyView;
    @ViewInject(R.id.search_hot_view)
    LinearLayout hotView;
    @ViewInject(R.id.delete_search_history)
    ImageView deleteImage;

    private String[] hotArr;

    private boolean isGoShopList = false;


    MCallback getHotKeywordCallback = new MCallback() {
        @Override
        public void onSuccess(String data) {
            hotArr = data.split(",");
            initHotView();
        }

        @Override
        public void onSuccess(String message, String code) {

        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {

        }

        @Override
        public void onCancelled(Callback.CancelledException cex) {

        }

        @Override
        public void onFinished() {
            dismisionDalog();
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isGoShopList = getIntent().getBooleanExtra("goShopList",false);
        x.view().inject(this);
        initHistoryView(false);
        getHotKeywords();
    }

    private void getHotKeywords() {
        if (showLoadingDialog()) {
            CommonHttp.commonHttp("hzg/mainData/hotKeywords", null, getHotKeywordCallback);
        }
    }

    @Event(R.id.search_text)
    private void onClick(View view) {
        search(searchEdit.getText().toString());
    }

    @Event(R.id.delete_search_history)
    private void delete(View view) {
        SearchHistoryDao.getInstance().deleteAll();
    }

    private void search(String keyword) {
        SearchHistoryDao.getInstance().save(keyword);
        if (!isGoShopList) {
            getIntent().putExtra("keyword", keyword);
            setResult(RESULT_OK, getIntent());
        } else {
            Intent intent = new Intent(this, ShopListActivity.class);
            intent.putExtra("title", keyword);
            intent.putExtra("keyword", keyword);
            startActivity(intent);
        }
        this.finish();
    }

    private void initHotView() {
        SearchView hotSearchView = new SearchView(this, hotArr, new KeywordClickListener());
        hotView.addView(hotSearchView);
    }

    private void initHistoryView(boolean needClear) {
        if (needClear)
            historyView.removeAllViews();
        String[] arr = SearchHistoryDao.getInstance().getAllHistory();
        if (arr == null || arr.length == 0) {
            deleteImage.setVisibility(View.GONE);
            historyView.setVisibility(View.GONE);
        }
        SearchView hSearchView = new SearchView(this, arr, new KeywordClickListener());
        historyView.addView(hSearchView);
    }

    class KeywordClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            String keyword = ((TextView) view).getText().toString();
            search(keyword);

        }
    }
}
